<?php
require_once("$libdir/chat_functs.$phpEx");

function showHeader($body = array(), $headers = '', $jsnames = array()) {
    global  $config;
    global  $full_header;
    global  $admin_header;
    global  $phpEx;

    if (defined('HEADER_LOADED')) {
        return;
    }

    if ($admin_header) {
        $header_tpl = 'admin/admin_header.tpl';
    } else {
        $header_tpl = 'overall_header.tpl';
    }

    // 
    // Load template header
    //
    $template = new Template($config['TPL_DIR']);
    $template->set_filenames(array('header' => $header_tpl));
    
    $body_class     = 'class=main ';
    $body_bgcolor   = '';
    $body_id        = '';
    $body_text      = '';
    $body_onload    = '';
    
    if (isset($body['class'])) {
        $body_class = "class={$body['class']} ";
    }
    
    if (isset($body['id'])) {
        $body_id    = "id={$body['id']} ";
    }
    
    if (isset($body['bgcolor'])) {
        if (substr($body['bgcolor'], 0, 1) == '#') {
            $body_bgcolor   = "bgcolor={$body['bgcolor']} ";
        } else {
            $body_bgcolor   = "bgcolor=#{$body['bgcolor']} ";
        }
    }
    
    if (isset($body['text'])) {
        $body_text  = "\n" . $body['text'];
    }

    if (isset($body['onload'])) {
        $body_onload    = " onLoad='" . $body['onload'] . "'";
    }

    $template->assign_vars(array(
        'FORUM_NAME'    => $config['FORUM_NAME'],
        'CSS'       => $config['TPL_WWW'] . $config['TEMPLATE'] . ".css",
        'CLASS'     => $body_class,
        'ID'        => $body_id,
        'BGCOLOR'   => $body_bgcolor,
        'TEXT'      => $body_text,
        'ONLOAD'    => $body_onload
    ));
    
    $baseurl    = getBaseURI();
    foreach ($jsnames as $jsname) {
        $template->assign_block_vars('switch_js', array(
            'U_BASEURL' => $baseurl,
            'JSNAME'    => $jsname,
        ));
    }

    if (is_array($headers)) {
        foreach ($headers as $thisHeader) {
            $template->assign_block_vars('switch_headers', $thisHeader);
        }
    }
    
    $adjust = explode("/", $_SERVER['PHP_SELF']);
    $page   = $adjust[count($adjust)-1];

    if ($full_header) {
        $template->assign_block_vars('switch_fullheader', array());

        $links['index']     = array('NAME'  => "Home",
                        'URL'   => "index.$phpEx");

        $links['transcripts']   = array('NAME'  => "Transcripts",
                        'URL'   => "transcripts.$phpEx");

        $links['whoson']    = array('NAME'  => "Who's On",
                        'URL'   => "whoson.$phpEx");

        $links['help']      = array('NAME'  => "Help Guide",
                        'URL'   => "help.$phpEx");

        foreach ($links as $link) {
            $template->assign_block_vars('switch_fullheader.header_link', $link);
            if ($link['URL'] != $page) {
                $template->assign_block_vars('switch_fullheader.header_link.switch_link', array());
            }
        }
        $tplHead    = "switch_fullheader.";
    }

    if ($admin_header) {
        if ($_SERVER['QUERY_STRING']) {
            $page .= "?" . $_SERVER['QUERY_STRING'];
        }

        $nav    = array(
                'General'               => array(
                    'Home'              => "index.$phpEx?op=",
                    'Chat Home'         => "../index.$phpEx",
                    'Who\'s On'         => "../whoson.$phpEx",
                ),
                'Configuration'         => array(
                    'Announcements'     => "index.$phpEx?op=config_announce",
                    'Cron Settings'     => "index.$phpEx?op=config_cron",
                    'General Settings'  => "index.$phpEx?op=config_settings",
                    'Plugins'           => "index.$phpEx?op=config_plugins",
                    'Server Methods'    => "index.$phpEx?op=config_cpsp",
                    'Timeouts'          => "index.$phpEx?op=config_timeouts",
                ),
                'Logs'  => array(
                    'Configure'         => "index.$phpEx?op=log_config",
                    'View'              => "index.$phpEx?op=log_view",
                ),
                'Manage'                => array(
                    'Ban Control'       => "index.$phpEx?op=ban_control",
                    'Rooms'             => "index.$phpEx?op=manage_rooms",
                    'Room Groups'       => "index.$phpEx?op=manage_groups",
                ),
                'Maintenance'           => array(
                    'Backup DB'         => "index.$phpEx?op=maint_backup",
                    'Offline Mode'      => "index.$phpEx?op=maint_mode",
                    'Purge Events'      => "index.$phpEx?op=maint_purge",
                ),
                'Transcripts'  => array(
                    'Search'            => "index.$phpEx?op=ts_search",
                    'Schedule'          => "index.$phpEx?op=ts_schedule",
                ),
            );
        $tplHead    = "";
    }

    if (isset($nav)) {
        foreach ($nav as $hname => $hlinks) {
            $template->assign_block_vars($tplHead . 'header_group', array('NAME' => $hname));
            foreach ($hlinks as $linkName => $linkURL) {
                $template->assign_block_vars($tplHead . 'header_group.header_link', array('NAME' => $linkName));
                if ($linkURL != $page) {
                    $template->assign_block_vars($tplHead . 'header_group.header_link.switch_link', array('URL' => $linkURL));
                }
            }
        }
    }
    $template->pparse('header');
    define('HEADER_LOADED', 1);
}
